package io.dshubs.archives.controller;


import io.dshubs.archives.entity.ArchiveTransferList;
import io.dshubs.archives.entity.vo.ArchiveTransferListStatusVo;
import io.dshubs.archives.service.ArchiveTransferListService;
import io.dshubs.commons.log.annotation.LogOperation;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.exception.RenException;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.Result;
import io.dshubs.commons.tools.validator.ValidatorUtils;
import io.dshubs.commons.tools.validator.group.AddGroup;
import io.dshubs.commons.tools.validator.group.DefaultGroup;
import io.dshubs.commons.tools.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案移交记录表 前端控制器
 * </p>
 *
 * @author tan
 * @since 2021-08-26
 */
@Api(tags = "档案移交记录")
@RestController
@RequestMapping("/transfer-list")
public class ArchiveTransferListController {
    @Autowired
    private ArchiveTransferListService archiveTransferListService;

    @GetMapping("page")
    @ApiOperation("档案移交:page")
    @LogOperation("档案移交:/transfer/transfer:/img/icon-档案移交.png:page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = "batchNo", value = "批次号", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "transferTitle", value = "标题", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "transferState", value = "移交状态", paramType = "query", dataType="String")
    })
    public Result<PageData<ArchiveTransferList>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<PageData<ArchiveTransferList>>().ok(archiveTransferListService.page(params));
    }

    @GetMapping("/{id}")
    @ApiOperation("详情")
    @LogOperation("档案移交:get")
    public Result<ArchiveTransferList> get(@PathVariable("id") Long id){
        return new Result<ArchiveTransferList>().ok(archiveTransferListService.selectById(id));
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("档案移交:save")
    public Result save(@RequestBody ArchiveTransferList archiveTransferList){
        List<ArchiveTransferList> res = new ArrayList<>();
        List<String> fileNumberList = archiveTransferList.getFileNumberList();
        if(CollectionUtils.isNotEmpty(fileNumberList)) {
            fileNumberList.forEach(item->{
                archiveTransferList.setFileNumber(item);
                archiveTransferList.setBatchNo("YJ-"+System.currentTimeMillis());
                res.add(archiveTransferList);
            });
            archiveTransferListService.insertBatch(res);
        } else {
            throw new RenException("未选择移交文件");
        }

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("档案移交:update")
    public Result update(@RequestBody ArchiveTransferList archiveTransferList){
        //效验数据
        ValidatorUtils.validateEntity(archiveTransferList, UpdateGroup.class, DefaultGroup.class);

        archiveTransferListService.updateById(archiveTransferList);

        return new Result();
    }

    @DeleteMapping()
    @ApiOperation("删除")
    @LogOperation("档案移交:delete")
    public Result delete(@RequestBody @NotNull(message = "批量删除id不能为空") List<Long> ids){
        archiveTransferListService.deleteBatchIds(ids);
        return new Result();
    }

    @GetMapping("/get/status")
    @ApiOperation("获取状态")
    @LogOperation("档案移交:get")
    public Result<ArchiveTransferListStatusVo> get(){
        return new Result<ArchiveTransferListStatusVo>().ok(archiveTransferListService.getCountStatus());
    }
}
